﻿//************************************************************************//
//文件功能：简单加解密算法的类
//创 建 人：薄帅
//创建时间：2010年08月04日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace ProjBackEnd.Common.Helper
{
    public class SimpleCrypt
    {
        /// <summary>
        /// 简单算法的字符串加密
        /// </summary>
        /// <param name="strArg">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string strArg)
        {
            byte[] btArr = Encoding.GetEncoding("gb2312").GetBytes(strArg);
            return Convert.ToBase64String(btArr);
        }
        /// <summary>
        /// 简单算法的字符串解密
        /// </summary>
        /// <param name="strArg">要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string strArg)
        {
            byte[] btArr = Convert.FromBase64String(strArg);
            return Encoding.GetEncoding("gb2312").GetString(btArr);
        }

        /// <summary>
        /// 微信小程序解密算法
        /// </summary>
        /// <param name="encryptedData">加密数据</param>
        /// <param name="iv">初始向量</param>
        /// <param name="sessionKey">从服务端获取的SessionKey</param>
        /// <returns></returns>
        public static string DecryptData(string encryptedData, string sessionKey, string iv)
        {
            try
            {
                //创建解密器生成工具实例
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                //设置解密器参数
                aes.Mode = CipherMode.CBC;
                aes.BlockSize = 128;
                aes.Padding = PaddingMode.PKCS7;
                //格式化待处理字符串
                byte[] byte_encryptedData = Convert.FromBase64String(encryptedData);
                byte[] byte_iv = Convert.FromBase64String(iv);
                byte[] byte_sessionKey = Convert.FromBase64String(sessionKey);

                aes.IV = byte_iv;
                aes.Key = byte_sessionKey;
                //根据设置好的数据生成解密器实例
                ICryptoTransform transform = aes.CreateDecryptor();

                //解密
                byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length);
                //生成结果
                string result = Encoding.UTF8.GetString(final);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }


        [DllImport("des64.dll")]
        private static extern void b64_des(StringBuilder in_str, StringBuilder out_str, string key, int lenth, uint option);
        [DllImport("des64.dll")]
        private static extern int b64_size(int lenth, uint option);

        /// <summary>
        /// 密码加密核心方法
        /// </summary>
        /// <param name="_strPassSource">密码明文</param>
        /// <returns></returns>
        public static string EncryptPassword(string _strPassSource)
        {
            string strKey = "proj+group+user-pwd";
            string strPassEncrypt = _strPassSource;

            int iOutlen = b64_size(_strPassSource.Length, 1);
            StringBuilder sbSource = new StringBuilder(_strPassSource);
            StringBuilder sbPassEncypt = new StringBuilder();
            sbPassEncypt.Capacity = iOutlen;
            sbPassEncypt.Length = iOutlen;
            b64_des(sbSource, sbPassEncypt, strKey, _strPassSource.Length, 1);
            strPassEncrypt = sbPassEncypt.ToString().Trim();
            return strPassEncrypt;
        }
    }
}
